<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Http\Controllers\Controller;
use App\Models\UserBill;
use App\Traits\Controllers\IndexTraits;
use App\Transformers\User\UserBillsTransformer;
use Illuminate\Http\Request;

class UserBillController extends Controller
{
    use IndexTraits;

    public $model = UserBill::class;

    public $transformer = UserBillsTransformer::class;

    public function index(Request $request)
    {
        //先处理传过来的搜索数据
        $this->formatIndexQuery($request);
        //获取开始时间和结束时间
        $start = $request->input('start');
        $stop = $request->input('stop');
        $name = $request->input('nickname');

        $data = $this
            ->getQuery()
            ->whereHas('user', function ($query) use ($name) {
                if(!empty($name)){
                    $query->where('nickname',$name);
                }
            })
            ->when($start && $stop,function ($query) use ($start, $stop){
                $query->whereBetween('created_at',[$start,$stop]);
            })
            ->pimp()//自动转换where条件
            ->paginate($this->getPageLimit());

        return $this->response->paginator($data, resolve($this->transformer));
    }


}
